package com.ssh;

import java.util.Arrays;
import java.util.PriorityQueue;

import org.junit.Test;

public class FindKthLargest_215 {
	@Test
	public void test() {
		int[] a = {3,1,5,7,1,9,6};
		Arrays.sort(a,1,3);
		System.out.println(Arrays.toString(a));
		
	}
	
	public int findKthLargest(int[] nums, int k) {
        Arrays.sort(nums);
        return nums[nums.length-k];
    }
	public int findKthLargest1(int[] nums, int k) {
	    PriorityQueue<Integer> pq = new PriorityQueue<>(); // 小顶堆
	    for (int val : nums) {
	        pq.add(val);
	        if (pq.size() > k) // 维护堆的大小为 K
	            pq.poll();
	    }
	    return pq.peek();
	}
}
